const ffi = require('ffi-napi');
const ref = require('ref-napi');

const dll_path = "E:\\workspace\\Cpp\\FHESearchOnEncryptedData\\x64\\Debug"
const dll = ffi.Library(`${dll_path}/FHESED4Customer.dll`, {
    dll_example: [ref.types.int, [ref.types.int]],
    read_aes_ecb_sk: [ref.refType(ref.types.char), [ref.refType(ref.types.char), ref.refType(ref.types.char), ref.refType(ref.types.int)]],
    aes_ecb_enc_utf8str: [ref.refType(ref.types.char), [ref.refType(ref.types.char), ref.refType(ref.types.char), ref.refType(ref.types.int)]],
    aes_ecb_dec_utf8str: [ref.refType(ref.types.char), [ref.refType(ref.types.char), ref.refType(ref.types.char), ref.refType(ref.types.int)]],
    read_soft_enc_pk: [ref.refType(ref.types.char), [ref.refType(ref.types.char), ref.refType(ref.types.char), ref.refType(ref.types.int)]],
    fhe_soft_enc_utf8str: [ref.refType(ref.types.char), [ref.refType(ref.types.char), ref.refType(ref.types.char), ref.refType(ref.types.int)]],
});

// dllexample
const dllexample_param = 12
const dllexample_ret = dll.dll_example(dllexample_param);
console.log(dllexample_ret)

// read_aes_ecb_sk
const aes_ecb_sk_file_path = "E:\\workspace\\Cpp\\FHESearchOnEncryptedData\\key\\huangheng20231101_aes_ecb_sk.txt"
const aes_ecb_sk_file_path_buf = ref.allocCString(aes_ecb_sk_file_path);
const read_aes_ecb_sk_res = Buffer.alloc(100);
const read_aes_ecb_sk_res_length = ref.alloc(ref.types.int);
dll.read_aes_ecb_sk(aes_ecb_sk_file_path_buf, read_aes_ecb_sk_res, read_aes_ecb_sk_res_length);
console.log("read_aes_ecb_sk_res", read_aes_ecb_sk_res.toString())
console.log("read_aes_ecb_sk_res_length", read_aes_ecb_sk_res_length.deref())

// aes_ecb_enc_utf8str
const aes_ecb_enc_utf8str_param = "中国"
const aes_ecb_enc_utf8str_param_buf = ref.allocCString(aes_ecb_enc_utf8str_param);
const aes_ecb_enc_utf8str_res = Buffer.alloc(100);
const aes_ecb_enc_utf8str_res_length = ref.alloc(ref.types.int);
dll.aes_ecb_enc_utf8str(aes_ecb_enc_utf8str_param_buf, aes_ecb_enc_utf8str_res, aes_ecb_enc_utf8str_res_length);
console.log("aes_ecb_enc_utf8str_res", aes_ecb_enc_utf8str_res.toString())
console.log("aes_ecb_enc_utf8str_res_length", aes_ecb_enc_utf8str_res_length.deref())

// aes_ecb_dec_utf8str
const aes_ecb_dec_utf8str_param = aes_ecb_enc_utf8str_res.toString()
const aes_ecb_dec_utf8str_param_buf = ref.allocCString(aes_ecb_dec_utf8str_param);
const aes_ecb_dec_utf8str_res = Buffer.alloc(100);
const aes_ecb_dec_utf8str_res_length = ref.alloc(ref.types.int);
dll.aes_ecb_dec_utf8str(aes_ecb_dec_utf8str_param_buf, aes_ecb_dec_utf8str_res, aes_ecb_dec_utf8str_res_length);
console.log("aes_ecb_dec_utf8str_res", aes_ecb_dec_utf8str_res.toString())
console.log("aes_ecb_dec_utf8str_res_length", aes_ecb_dec_utf8str_res_length.deref())

// read_soft_enc_pk
const fhe_pk_file_path = "E:\\workspace\\Cpp\\FHESearchOnEncryptedData\\key\\huangheng20231023_pk_key.txt"
const fhe_pk_file_path_buf = ref.allocCString(fhe_pk_file_path);
const read_soft_enc_pk_res = Buffer.alloc(100);
const read_soft_enc_pk_res_length = ref.alloc(ref.types.int);
dll.read_soft_enc_pk(fhe_pk_file_path_buf, read_soft_enc_pk_res, read_soft_enc_pk_res_length);
console.log("read_soft_enc_pk_res", read_soft_enc_pk_res.toString())
console.log("read_soft_enc_pk_res_length", read_soft_enc_pk_res_length.deref())

// fhe_soft_enc_utf8str
const fhe_soft_enc_utf8str_param = "中国人"
const fhe_soft_enc_utf8str_param_buf = ref.allocCString(fhe_soft_enc_utf8str_param);
const fhe_soft_enc_utf8str_res = Buffer.alloc(256*4);
const fhe_soft_enc_utf8str_res_length = ref.alloc(ref.types.int);
dll.fhe_soft_enc_utf8str(fhe_soft_enc_utf8str_param_buf, fhe_soft_enc_utf8str_res, fhe_soft_enc_utf8str_res_length);
console.log("fhe_soft_enc_utf8str_res", fhe_soft_enc_utf8str_res.toString())
console.log("fhe_soft_enc_utf8str_res_length", fhe_soft_enc_utf8str_res_length.deref())

// 参考文档
// https://www.npmjs.com/package/ffi-napi
// https://tootallnate.github.io/ref/

// 